<?php

namespace addon\hotel\api\controller;

use addon\hotel\model\RoomType as RoomTypeModel;
use addon\hotel\model\Room as RoomModel;
use app\api\controller\BaseApi;

class Room extends BaseApi
{

    //房间类型
    public function type()
    {
        $hotel_id = isset($this->params[ 'hotel_id' ]) ? $this->params[ 'hotel_id' ] : 0;

        $room_type_model = new RoomTypeModel();

        $condition = [
            ['hotel_id','=',$hotel_id]
        ];

        $list = $room_type_model->getRoomTypeList($condition,'type_id,hotel_id,type_name,baoyu_type_id');

        return $this->response($list);
    }

    //房间列表
    public function lists()
    {
        $baoyu_type_id = isset($this->params[ 'baoyu_type_id' ]) ? $this->params[ 'baoyu_type_id' ] : 0;
        $hotel_id = isset($this->params[ 'hotel_id' ]) ? $this->params[ 'hotel_id' ] : 0;

        $room_model = new RoomModel();

        $condition = [
            ['hotel_id','=',$hotel_id],
            ['status','=',1]
        ];

        if ($baoyu_type_id){
            $condition[] = ['type_id','=',$baoyu_type_id];
        }

        $field = '*';

        $list = $room_model->getRoomList($condition,$field);

        $token = $this->checkToken();

        foreach ($list['data'] as $k=>$v){
            $room_type = model('hotel_room_type')->getInfo(['baoyu_type_id'=>$v['type_id']]);
            $list['data'][$k]['room_type_info'] = $room_type;
            if ($token['code'] >= 0){
                // 是否参与会员等级折扣
                $goods_member_price = $room_model->getRoomPrice($v['room_id'], $this->member_id);
                $goods_member_price = $goods_member_price['data'];
                if (!empty($goods_member_price['member_price'])) {
                    $list['data'][$k]['member_price'] = $goods_member_price['member_price'];
                }
            }
        }
        return $this->response($list);
    }

    //房间详情
    public function info()
    {
        $room_id = isset($this->params[ 'room_id' ]) ? $this->params[ 'room_id' ] : 0;

        $room_model = new RoomModel();

        $condition = [
            ['room_id','=',$room_id],
            ['status','=',1]
        ];

        $info = $room_model->getNewRoomInfo($condition);

        if (empty($info['data'])){
            return $this->response($this->error('','房间不存在'));
        }
        $room_type = model('hotel_room_type')->getInfo(['baoyu_type_id'=>$info['data']['type_id']]);
        $info['data']['room_type_info'] = $room_type;

        $token = $this->checkToken();
        if ($token['code'] >= 0){
            // 是否参与会员等级折扣
            $goods_member_price = $room_model->getRoomPrice($room_id, $this->member_id);
            $goods_member_price = $goods_member_price['data'];
            if (!empty($goods_member_price['member_price'])) {
                $info['data']['member_price'] = $goods_member_price['member_price'];
            }
        }
        return $this->response($info);
    }

    //
}